public class test06 {
    public int strStr(String haystack, String needle) {
        int[] next = new int[needle.length()];
        getNext(next,needle);
        int j =0;
        for(int i =0;i<haystack.length();i++){
            while(j>0 && haystack.charAt(i)!=needle.charAt(j)){
                j = next[j-1];
            }
            if(haystack.charAt(i)==needle.charAt(j)){
                j++;
            }
            if(j==needle.length()){
                return i-j+1;
            }
        }
        return -1;
    }

    //前缀表
    private void getNext(int[] next,String s){
        int j=0;
        next[0] = 0;
        for(int i =1;i<s.length();i++){
            while(j>0 && s.charAt(j)!=s.charAt(i)){
                j = next[j-1];
            }
            if(s.charAt(j)==s.charAt(i)){
                j++;
            }
            //更新next数组的值
            next[i] = j;
        }
    }
}
